<?
class Beacon extends Root{
	var $Init = 0;
	var $Data;
	var $Nom;
	var $Result;
	var $Beacon;
	var $ChildObjects;
	var $ChildData;
	var $Child;
	var $Vars;
	var $TempVar;
	var $Path;
	var $Conf;
	function __construct(){
	}
	function __destruct() {
	}
	final public function __clone_array($d) {
		if (is_array($d))foreach ($d as $k2=>$d2){
			switch (gettype($d2)){
				case "int":
				case "string":
					$t[$k2] = $d2;
					break;
				case "array":
					$t[$k2] =$this->__clone_array($d2);
					break;
				case "object":
					$t[$k2] = $d2->__deepclone();
					break;
				default:
					break;
			}
		}
		return $t;
	}

	final public function __deepclone() {
		//Methode de clonage
		//On instancie un nouvel objet reflection pour avoir les information sur l objet a cloner.
		$class = get_class($this);
		$r = new ReflectionClass($class);
		//On cree un nouvel objet vide
		$temp = new $class;
		$Props = $r->getProperties();
		//On copie l ensemble des proprietes
		foreach ($Props as $p){
			$n = $p->name;
			switch (gettype($this->$n)){
				case "int":
				case "string":
					$temp->$n = $this->$n;
					break;
				case "array":
					$temp->$n =$this->__clone_array($this->$n);
					break;
				case "object":
					$temp->$n = clone $this->$n;
					break;
				default:
					break;
			}
		}
		return $temp;
	}

	function setFromVar($Var,$Data,$Beacon) {
		$this->Beacon = $Beacon["BEACON"];
		$this->Vars = $Var;
		$this->Data = $Data;
		//$this->init($Process);
	}

	function init($Process) {
		$this->Process($Process);
	}

	function setTempVar($Tab) {
		$this->TempVar = $Tab;
	}

	function Generate($Process,$Skin=false) {
		$this->Data = $Process->processingVars($this->Data);
		switch  ($this->Beacon) {
			case "MODULE":
				//ON extrait les variables GET internes
				$TabTemp = explode("|",$this->Vars);
				$Test = explode("?",$TabTemp[0]);
				if (is_array($Process->TempVar)) foreach ($Process->TempVar as $Te=>$Tr) $Pro[$Te] = $Tr;
				if (sizeof($Test)>1) {
					//Alors il y a des variables get
					$VarsToRegister = explode("&",$Test[1]);
					for ($i=0;$i<sizeof($VarsToRegister);$i++){
						$Temp = explode("=",$VarsToRegister[$i]);
						$Name = $Temp[0];
						$Value = $Temp[1];
						//$GLOBALS["Systeme"]->registerVar($Name,$Process->processingVars($Value));
						$Name = (sizeof(explode("[!",$Name))==1)?$Name:$Process->processingVars($Name);
						$Process->RegisterTempVar($Name,$Process->processingVars($Value));
					}
					$Lien = $Test[0];
				}
				$Lien = $Test[0];
				//ON genere le module si il n 'est pas generé
				$Vars = $Process->processingVars($Lien);
				if (!sizeof($this->ChildObjects)){
					preg_match("#(.*?)[.\/](.*)#", $Vars, $Out);
					if ($Out[1]!=""){
						$Module = $Out[1];
					}else $Module = $Vars;
					preg_match("#".$Module."\/(.*)#", $Vars, $Out);
					$Module = str_replace(" ","",$Module);
					if (!is_object($GLOBALS['Systeme']->Modules[$Module])){
						echo "<h1>LE MODULE $Module N'est pas installé pour la requete ".$this->Vars."</h1>";
						return false;
					}
					$GLOBALS['Systeme']->Modules[$Module]->loadSchema();
					$Bloc=$GLOBALS['Systeme']->Modules[$Module]->setData($Out[1]);
					$this->Conf =$Bloc->Conf;
					$this->ChildObjects=$Bloc->ChildObjects;
					$GLOBALS["Systeme"]->Log->Log("CHARGEMENT BEACON ".sizeof($Bloc->ChildObjects)." ".$Out[1]);
				}
				if (sizeof($this->Conf))foreach ($this->Conf as $K=>$V){
					$V = $Process->processingVars($V);
					$Process->RegisterTempVar($K,$V);
				}
				$this->Beacon="";
				$this->Generate($Process);
				if ($TabTemp[1]!="GLOBAL")$Process->TempVar = $Pro ;
			break;
			case "LANG":
				//ON extrait les variables GET internes
				$TabTemp = explode("|",$this->Vars);
				$Test = explode("?",$TabTemp[0]);
				if (sizeof($Test)>1) {
					//Alors il y a des variables get
					$VarsToRegister = $Test[1];
					$VarsToRegister = explode("&",$VarsToRegister);
					for ($i=0;$i<sizeof($VarsToRegister);$i++){
						$Temp = explode("=",$VarsToRegister[$i]);
						$Name = $Temp[0];
						$Value = $Temp[1];
						//$GLOBALS["Systeme"]->registerVar($Name,$Process->processingVars($Value));
						$Process->RegisterTempVar($Name,$Process->processingVars($Value));
					}
					$Lien = $Test[0];
				}
				$Lien = $Test[0];
				//ON genere le module
				$Vars = $Process->processingVars($Lien);
				preg_match("#(.*?)[.\/](.*)#", $Vars, $Out);
				if ($Out[1]!=""){
					$Module = $Out[1];
				}else $Module = $Vars;
				preg_match("#".$Module."\/(.*)#", $Vars, $Out);
				$Module = str_replace(" ","",$Module);
				//  			echo "MODULE ".$Module." OUT ".$Out[1]." PATH ".$this->Beacon."\r\n";
				$GLOBALS['Systeme']->Modules[$Module]->loadSchema();
				$Bloc=$GLOBALS['Systeme']->Modules[$Module]->setData($Out[1]);
				$T =$Bloc->Conf;
				if (sizeof($T))foreach ($T as $K=>$V){
					$V = $Process->processingVars($V);
					$Process->RegisterTempVar($K,$V);
				}
				$Process=$Bloc->Generate($Process);
				$this->ChildObjects[]=$Bloc;
				break;
			default:
				// 			$this->Vars = preg_replace('#\[!([\w\W\s]*?)!\]#e','$Process->processVars("\1")' , $this->Vars);
				$Vars = $Process->processingVars($this->Vars);
				$this->Vars = $Process->PostProcessing($this->Vars);
				if (sizeof($this->BlObjects)) for ($i=0;$i<sizeof($this->BlObjects);$i++) if (is_object($this->BlObjects[$i])) {
					$Process=$this->BlObjects[$i]->Generate($Process,$Skin);
				}else{
					$this->BlObjects[$i] = $Process->processingVars($this->BlObjects[$i]);
					$this->BlObjects[$i]=$Process->PostProcessing($this->BlObjects[$i]);
				}
				if (sizeof($this->ChildObjects)) for ($i=0;$i<sizeof($this->ChildObjects);$i++){
					if (is_object($this->ChildObjects[$i])){
						$Process=$this->ChildObjects[$i]->Generate($Process,$Skin);
					}else{
						$this->ChildObjects[$i] = $Process->processingVars($this->ChildObjects[$i]);
						$this->ChildObjects[$i]=$Process->PostProcessing($this->ChildObjects[$i]);
					}
				}
				break;
		}
		return $Process;
	}

	function writeCacheFile($Data,$Url,$Name) {
	// 		$Obj = new genericClass("Explorateur");
	// 		$Obj->initFromType("Fichier");
	//  		$Obj->Set("Url","Dossier/".$Url);
	//  		$Obj->Set("Nom",$Name);
	// 		$Obj->Set("Contenu",$Data);
	// 		$Obj->Save();
	//		$Url = str_replace("/","_",$Url);
	if (!$File=fopen ($Url."/".$Name,"w"))return false;
	fwrite($File,$Data);
	fclose($File);	}

	function writeCache($Url,$Id="Skin") {
		//On commence par repercuter la commande pour que le processus commence du bout des branches
		for($i=0;$i<sizeof($this->ChildObjects);$i++) {
			if (is_object($this->ChildObjects[$i]))$this->ChildObjects[$i]->writeCache($Url,$Id."-".$i."-".$this->ChildObjects[$i]->Beacon);
		}
		//On ecrit les fichiers
		if ($Id=="Skin")$this->writeCacheFile(serialize($this),$Url,$Id.".cache");
	}

	function searchChild($Beacon,$Child,$First=true,$level=0) {
		// 		if ($level==0&&$First) echo "------------------START-----|$First|----------\r\n";
		$test=false;
		if (gettype($Beacon)=="string") {$BeaconTemp[] = $Beacon;$Beacon = $BeaconTemp;}
		// 		for ($i=0;$i<$level;$i++) echo "--";
		// 		echo "-->RECHERCHE $Child | Balise bloquante $Beacon | BALISE EN COURS ".$this->Beacon." | LEVEL  ".$level."\r\n";
		if (($this->Beacon!=$Child&&!in_array($this->Beacon,$Beacon))||$First) {
			if (is_array($this->ChildObjects)) foreach ($this->ChildObjects as $Key) {
				if (!$test&&is_object($Key)) {
					// 					for ($i=0;$i<$level;$i++) echo "--";
					// 					echo "--->".$Key->Beacon." \r\n";
					$temp = $level+1;
					$test=$Key->searchChild($Beacon,$Child,false,$temp);
				}else{
					// 					for ($i=0;$i<$level;$i++) echo "--";
					// 					echo "-->__PAS D'OBJET\r\n";
				}
			}else{
				// 				for ($i=0;$i<$level;$i++) echo "--";
				// 				echo "-->__PAS DE BALISE FILLE\r\n";
			}
		}else{
			if ($this->Beacon==$Child) {
				$test=true;
				// 				for ($i=0;$i<$level;$i++) echo "--";
				// 				echo "-->//////////// C la Bonne".$this->Beacon." \r\n";
			}else {
				$test=false;
				// 				for ($i=0;$i<$level;$i++) echo "--";
				// 				echo "-->!!!!!!! BALISE BLOQUANTE ".$this->Beacon." level $level | $First\r\n";
			}
		}

		// 		if ($test&&$First)echo "-->!!!!!BINGO!!!! $Child BAL EN COURS ".$this->Beacon." \r\n";
		return $test;
	}

	function getObjVars() {
		if (sizeof($this->ChildObjects)>0) {
			foreach ($this->ChildObjects as $Key) {
				if (is_object($Key)){
					$ObjVars = $Key->getObjVars();
					if (sizeof($ObjVars)) {
						array_push($this->ObjVars,$ObjVars);
					}
				}
			}
		}
		if (sizeof($this->BlObjects)>0) {
			foreach ($this->BlObjects as $Key) {
				if (is_object($Key)){
					$ObjVars = $Key->getObjVars();
					if (sizeof($ObjVars)) {array_push($this->ObjVars,$ObjVars); }
				}
			}
		}
		return $this->ObjVars;
	}


	function setChild($Child) {
		$this->Child = $Child;
	}

	function getModulesLoaded($Result = Array()) {
		if (!empty($this->Path)) {
			//On verifie que l entree n y est pas deja
			$test=false;
			foreach ($Result as $mods) {
				if ($mods==$this->Path)$test=true;
			}
			if (!$test) $Result[] = $this->Path;
		}
		if (sizeof($this->ChildObjects)) {
			foreach ($this->ChildObjects as $Key) {
				if (is_object($Key))$Result = $Key->getModulesLoaded($Result);
			}
		}
		if (sizeof($this->BlObjects)) {
			foreach ($this->BlObjects as $Key) {
				if (is_object($Key))$Result = $Key->getModulesLoaded($Result);
			}
		}
		return $Result;
	}

	function getContent($TabObj) {
		$Data="";
		if (is_array($TabObj)) {
			for ($i=0;$i<count($TabObj);$i++) {
				if (is_object($TabObj[$i])) {
					$Data .= $TabObj[$i]->Affich();
				}else{
					$Data .= $TabObj[$i];
				}
			}
		}
		return $Data;
	}

	function Process($Process) {
 		if ($this->Data!="")$this->ChildObjects= $Process->Processing($this->Data,false);
		$this->PostObjects = $Process->PostObjects;
		$this->Data="";
	}

	function parseData($Replace,$Content){
		$Data = str_replace('[DATA]',$Replace , $Content);
		return $Data;
	}

	function getChild($Beacon,$Child,$First=true,$Block=true,$level=0) {
		// 		if ($level==0&&$First) echo "------------------START-----|$First|----------\r\n";
		$test=false;
		if (gettype($Beacon)=="string") {$BeaconTemp[] = $Beacon;$Beacon = $BeaconTemp;}
		// 		for ($i=0;$i<$level;$i++) echo "--";
		// 		echo "-->RECHERCHE $Child | Balise bloquante $Beacon | BALISE EN COURS ".$this->Beacon." | LEVEL  ".$level."\r\n";
		if (($this->Beacon!=$Child&&!in_array($this->Beacon,$Beacon))||$First) {
			if (is_array($this->ChildObjects)) foreach ($this->ChildObjects as $Key) {
				if (!$test&&is_object($Key)) {
					// 					for ($i=0;$i<$level;$i++) echo "--";
					// 					echo "--->".$Key->Beacon." \r\n";
					$test=$Key->getChild($Beacon,$Child,false,$Block,$level+1);
				}else{
					// 					for ($i=0;$i<$level;$i++) echo "--";
					// 					echo "-->__PAS D'OBJET\r\n";
				}
			}else{
				// 				for ($i=0;$i<$level;$i++) echo "--";
				// 				echo "-->__PAS DE BALISE FILLE\r\n";
			}
		}else{
			if ($this->Beacon==$Child) {
				return $this;
				// 				for ($i=0;$i<$level;$i++) echo "--";
				// 				echo "-->//////////// C la Bonne".$this->Beacon." \r\n";
			}else {
				$test=false;
				// 				for ($i=0;$i<$level;$i++) echo "--";
				// 				echo "-->!!!!!!! BALISE BLOQUANTE ".$this->Beacon." level $level | $First\r\n";
			}
		}

		// 		if ($test&&$First)echo "-->!!!!!BINGO!!!! $Child BAL EN COURS ".$this->Beacon." \r\n";
		return $test;
	}

	function getAllChild($Beacon,$Child,$First=true,$Block=false,$level=0) {
		$Result=false;
		// 		for ($i=0;$i<$level;$i++) echo "--";
		// 		echo ">GET CHILD RECH $Child Depart Balise ".$this->Beacon." Child ".$this->Child." level ".$level."\r\n";
		if ($this->Beacon!=$Child||$First||$this->Child!=$Beacon) {
			if (sizeof($this->ChildObjects))foreach ($this->ChildObjects as $Key) {
				if (is_object($Key)){
					if ($Block&&!$First){
						// 						for ($i=0;$i<$level;$i++) echo "--";
						// 						echo ">Balise ".$this->Beacon." Child ".$this->Child." level ".$level."\r\n";
						if ($Key->Beacon!=$Child){
							//La balise est differente de la balise bloquante
							// 							for ($i=0;$i<$level;$i++) echo "--";
							// 							echo "Balise differente Balise ".$Beacon." Child ".$Key->Beacon."";
							$test=$Key->getAllChild($Beacon,$Child,false,$Block,$level+1);
							if (is_object($test)) $Result[]=$test;
							$test=false;
						}else{
							//La Balise est identique a la balise bloquante
							if ($Key->Child!=$Beacon){
								if ($Key->Child!="") {
									//Balise identique mais il s agit d'une balise enfant donc on continue
									// 									for ($i=0;$i<$level;$i++) echo "--";
									// 									echo "Balise bloquante mais Child different ".$Child." Child ".$Key->Child."";
									$test=$Key->getAllChild($Beacon,$Child,false,$Block,$level+1);
									if (is_object($test)) $Result[]=$test;
									$test=false;
								}else{
									//Cas de la balise bloquante
									// 									for ($i=0;$i<$level;$i++) echo "--";
									// 									echo ">Balise Bloquante level:$level --> balBlock".$Beacon." Child ".$this->Child." - BalTrouve ".$Key->Beacon."\r\n";
								}
							}else{
								$test=$Key->getAllChild($Beacon,$Child,false,$Block,$level+1);
								if (is_object($test)) $Result[]=$test;
								$test=false;
							}
						}
					}else{
						$test=$Key->getAllChild($Beacon,$Child,false,$Block,$level+1);
						if (is_object($test)) $Result[]=$test;
						$test=false;
					}
				}else{
				}
			}
		}elseif($this->Beacon==$Child){
			$Result=$this;
			// 			if ($test) echo "ON a l objet\r\n";
		}
		// 		for ($i=0;$i<$level;$i++) echo "--";
		// 		echo "Fin ".$this->Beacon." ".$this->Child." $level // $test\r\n";
		return $Result;

	}
	function loadCacheFile($FileName){
		return unserialize(file_get_contents($this->URL));
	}

	function affich() {
	}
}

?>